$include("js/ajax.js");
$include("/com/opesoft/pf/jpa/listCtrlAction.ojs");

function ListCtrlConfig(dataGridName)
{
	this.dataGridName=dataGridName;  //用于显示数据的表格
	this.properties;

	this.listCtrl;  //具体的ListCtrl对象，根据分页的方式不同，抽象成不同的ListCtrl

	this._w=6;  //一个数字的宽度

	this.msg={
		first:"First Page",
		back:"Back",
		next:"Forward",
		last:"Last Page",
		reload:"Reload List",		
		noNext:"Already at last page!"
	};

	var self=this;

	this.next=function()
	{
		var cd=self.listCtrl.ctrlData;
		if(cd.allPage!=null)  //已经加载过数据
		{
			if(cd.nowPage<cd.allPage)
				self.listCtrl.toPage(cd.nowPage+1);
			else
				$info(this.msg.noNext);
		}
	}

	this.back=function()
	{
		var cd=self.listCtrl.ctrlData;
		if(cd.allPage!=null)  //已经加载过数据
		{
			if(cd.nowPage>1)
				self.listCtrl.toPage(cd.nowPage-1);
			else
				$info("已经是第一页了。");
		}
	}

	this.first=function()
	{
		var cd=self.listCtrl.ctrlData;
		if(cd.allPage!=null)  //已经加载过数据
		{
			if(cd.nowPage>1)
				self.listCtrl.toPage(1);
			else
				$info("已经是第一页了。");
		}
	}

	this.last=function()
	{
		var cd=self.listCtrl.ctrlData;
		if(cd.allPage!=null)  //已经加载过数据
		{
			if(cd.nowPage<cd.allPage)
				self.listCtrl.toPage(cd.allPage);
			else
				$info("已经是最后一页了。");
		}
	}

	this.changeSize=function()
	{
		if(self.listCtrl!=null)
		{
			var cd=self.listCtrl.ctrlData;
			if(cd.allPage!=null)  //已经加载过数据
			{
				var pageSize=parseInt(self.pageSizeInput.value);
				if(!isNaN(pageSize))
				{
					if(pageSize>2000)
						pageSize=2000;
					else if(pageSize<1)
						pageSize=1;

					cd.pageSize=pageSize;
					self.listCtrl.reload();
				}
				else
					self.pageSizeInput.value=cd.pageSize;
			}
		}
	}

	this.changePageNo=function()
	{
		if(self.listCtrl!=null)
		{
			var cd=self.listCtrl.ctrlData;
			if(cd.allPage!=null)  //已经加载过数据
			{
				var nowPage=parseInt(self.nowPageInput.value);
				if(!isNaN(nowPage))
				{
					if(nowPage!=cd.nowPage)
					{
						if(nowPage>cd.allPage)
							nowPage=cd.allPage;
						else if(nowPage<1)
							nowPage=1;

						self.listCtrl.toPage(nowPage);
					}
				}
				else
					self.nowPageInput.value=cd.nowPage;
			}
		}
	}

	this.reload=function()
	{
		if(self.listCtrl!=null)
			self.listCtrl.reload();
	}

	this.changeInputSize=function()
	{
		this.style.width=this.value.length*self._w+10;
	}

}

ListCtrlConfig.prototype.init=function(firstButton,backButton,nextButton,lastButton,reloadButton,nowPageInput,allPageInput,pageSizeInput,allRecordInput)
{
	this.firstButton=firstButton;
	this.backButton=backButton;
	this.nextButton=nextButton;
	this.lastButton=lastButton;
	this.reloadButton=reloadButton;
	this.nowPageInput=nowPageInput;
	this.allPageInput=allPageInput;
	this.pageSizeInput=pageSizeInput;
	this.allRecordInput=allRecordInput;

	this._hiddenButtons();

	if($_.IE)
	{
		this.firstButton.alt=	this.msg.first;
		this.backButton.alt=	this.msg.back;
		this.nextButton.alt=	this.msg.next;
		this.lastButton.alt=	this.msg.last;
		this.reloadButton.alt=	this.msg.reload;
	}
	else
	{
		this.firstButton.title=	this.msg.first; 
		this.backButton.title=	this.msg.back;  
		this.nextButton.title=	this.msg.next;  
		this.lastButton.title=	this.msg.last;  
		this.reloadButton.title=this.msg.reload;
	}

	this.firstButton.onclick=this.first;
	this.backButton.onclick=this.back;
	this.nextButton.onclick=this.next;
	this.lastButton.onclick=this.last;
	this.reloadButton.onclick=this.reload;
	this.pageSizeInput.onchange=this.changeSize;
	this.pageSizeInput.onkeyup=this.changeInputSize;
	this.nowPageInput.onchange=this.changePageNo;
	this.nowPageInput.onkeyup=this.changeInputSize;

	this.nowPageInput.value=0;
	this.allPageInput.value=0;
	this.pageSizeInput.value=0;
}

ListCtrlConfig.prototype._hiddenButtons=function()
{
	this.firstButton.style.display='none';
	this.backButton.style.display='none';
	this.nextButton.style.display='none';
	this.lastButton.style.display='none';
}

ListCtrlConfig.prototype.setData=function(data,properties)
{
	this.properties=properties;

	if(data["class"].split("@")[0]=="com.opesoft.pf.page.ServerQueryResult")
	{
		//保留从前的PageSize属性
		var oldPageSize;
		if(this.listCtrl!=null) oldPageSize=this.listCtrl.ctrlData.pageSize;
		this.listCtrl=new ServerListCtrl();  //新建ListCtrl对象
		if(oldPageSize!=null) this.listCtrl.ctrlData.pageSize=oldPageSize;

		//初始化服务器端分页数据集
		this.listCtrl.countQL=data.countQL;
		this.listCtrl.queryQL=data.queryQL;
		this.listCtrl.config=this;
	}

	this.listCtrl.query();
}

ListCtrlConfig.prototype.showState=function(ctrlData)
{
	this._hiddenButtons();

	if(ctrlData.nowPage>1)
	{
		this.firstButton.style.display='';
		this.backButton.style.display='';
	}
	
	if(ctrlData.nowPage<ctrlData.allPage)
	{
		this.nextButton.style.display='';
		this.lastButton.style.display='';
	}

	this.nowPageInput.value=ctrlData.nowPage;
	this.nowPageInput.style.width=(ctrlData.nowPage+"").length*this._w+10;
	this.allPageInput.value=ctrlData.allPage;
	this.allPageInput.style.width=(ctrlData.allPage+"").length*this._w+10;
	this.pageSizeInput.value=ctrlData.pageSize;
	this.pageSizeInput.style.width=(ctrlData.pageSize+"").length*this._w+10;
	this.allRecordInput.value=ctrlData.allRecord;
	this.allRecordInput.style.width=(ctrlData.allRecord+"").length*this._w+10;
}


/*
处理服务器端分页QueryResult的ListCtrl对象  */
function ServerListCtrl()
{
	this.config;
	this.countQL;
	this.queryQL;

	//保存分页状态的数据
	this.ctrlData={
		"nowPage":1,
		"pageSize":100
	};

	var self=this;

	this._query=function(result)
	{
		var dataGrid=window[self.config.dataGridName];
		if(dataGrid!=null && result!=null)
		{
			self.ctrlData=result.listCtrl;
			self.config.showState(self.ctrlData);

			dataGrid.data=result.data;
			dataGrid.reload();
		}
	}
}

ServerListCtrl.prototype.query=function()
{
	ListCtrlAction.serverQuery(this.countQL,this.queryQL,this.config.properties,this.ctrlData.nowPage,this.ctrlData.pageSize,this._query);
}

ServerListCtrl.prototype.reload=function()
{
	this.query();
}

ServerListCtrl.prototype.toPage=function(nowPage)
{
	this.ctrlData.nowPage=nowPage;
	this.query();
}
